﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Models;
using System.Data;
using System.Data.SqlClient;
using DBUnitility;
namespace DAL
{
    public class SysAdminService
    {
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="objSysAdmin"></param>
        /// <returns></returns>
        public SysAdmins SysAdminLogin(SysAdmins objSysAdmin)
        {
            //【1】封装参数
            SqlParameter[] param = new SqlParameter[]
           {
                new SqlParameter("@LoginId",objSysAdmin.LoginId),
                new SqlParameter("@LoginPwd",objSysAdmin.LoginPwd)
           };
            //【2】调用带参数的存储过程
            SqlDataReader objReader = SQLHelper.GetReaderByProcedure("usp_SysAdmins", param);
            //【3】读取数据
            if (objReader.Read())
            {
                objSysAdmin.AdminName = objReader["AdminName"].ToString();
                objSysAdmin.AdminStatus = Convert.ToInt32(objReader["AdminStatus"]);
                objSysAdmin.RoleId = Convert.ToInt32(objReader["RoleId"]);
            }
            else
            {
                objSysAdmin = null;
            }
            objReader.Close();
            return objSysAdmin;
        }
        /// <summary>
        /// 写入登录信息
        /// </summary>
        /// <param name="objLoginLogs"></param>
        /// <returns></returns>
        public int WriteLoginInfo(LoginLogs objLoginLogs)
        {
            string sql = "insert into LoginLogs(LoginId,SPName,ServerName,LoginTime) values(@LoginId,@SPName,@ServerName,@LoginTime);select @@identity";
            SqlParameter[] param = new SqlParameter[]
           {
                new SqlParameter ("@LoginId",objLoginLogs.LoginId),
                new SqlParameter ("@SPName",objLoginLogs.SPName),
                new SqlParameter ("@ServerName",objLoginLogs.ServerName),
                new SqlParameter ("@LoginTime",objLoginLogs.LoginTime),
           };
            return Convert.ToInt32(SQLHelper.GetSingalResult(sql, param));
        }
        /// <summary>
        /// 写入退出时间
        /// </summary>
        /// <param name="logId"></param>
        /// <returns></returns>
        public int WriteLogOutInfo(string logId)
        {
            string sql = "update LoginLogs set ExitTime ='{0}' where LogId = {1}";
            sql = string.Format(sql, SQLHelper.GetDBServerTime(), logId);
            return SQLHelper.Update(sql);
        }
    }
}
